<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;

use Session;
use App\Shop;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    
    /**
     * Display a list ing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('Shop.login');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function doLogin(Request $request)
    { 
        $code = $request->get('code');
        $value = $request->session()->get('milkcaptcha');
        // $value = $request->session()->all();
        if($code==$value) {
            //用户输入验证码错误
            $sname = $request->sname;
            $user = Shop::where('sname',$sname)->first();
            //判断 
            if(empty($user)){
                return back()->with('info','店铺不存在'); 
            }

            $status = $user->status;
            if($status == '0'){
                return back()->with('info','店铺被关闭');
            }
            //判断密码
            if($request->password==$user->password){
                // session(['id'=>$user->id]);
                $request->session()->put('sname', $user);
                
                return redirect('/shop')->with('info','登录成功');
            }else{
                return back()->with('info','密码错误');
            }
        } else {
            return back()->with('info','密码错误');
            
        }

    }

    public function logout(Request $request)
    {
        $request->session()->flush();
        return redirect('/shop/login');
    }
}
